<?php

namespace app\public_module\controller;

use \app\common\controller\Base;
use \think\Validate;


class Index extends Base
{
    /*
     * 继承父类的_initialize()
     */
    public function _initialize()
    {
        parent::_initialize();
    }


    public function index()
    {
        $this->redirect("public_module/index/show_list");//页面的跳转
    }

    /*
     * param show_list  员工列表展示
     * @return \think\response\View
     */
    public function show_list()
    {
        $order_name = input("?order_name") ? input("order_name") : "DESC";//按照名字排序
        $now_staff = input("now_staff");//当前的员工id
        $name = input("name");//职员名字
        $now_name = input("now_name");//咨询者名称
        if (!session('common_show_all_data')) {
            $term['s.id'] = session("user_id");
        }
        $term['s.status'] = "1";
        $term['s.name'] = ['like', "%" . $name . "%"];
        $staff_list = db('staff')
            ->alias('s')
            ->join('department d', 'd.id = department_id')
            ->where($term)
            ->order(['s.name' => $order_name, 's.id' => "desc"])
            ->field('s.id,s.name,d.name as department_name,s.private_qq,s.work_qq,s.private_wechat,s.work_wechat,s.private_tel,s.work_tel')
            ->paginate(6);
        //这里输出的是一个数组
        $this->assign([
            'staff_list' => $staff_list
            , 'order_name' => $order_name
            , 'name' => $name
            , '$now_staff' => $now_staff
            , 'now_name' => $now_name
        ]);

        return view();
    }

    /*
     * 根据ID拿取职员信息
     */
    public function get_staff_info($now_staff = "")
    {
        $staff_list = db("staff")
            ->alias('s')
            ->join('education edu', 'edu.id = s.education_id', 'LEFT')
            ->join('department dep', 'dep.id = s.department_id', 'LEFT')
            ->join('departmentposition dp', 'dp.id = s.department_position_id', 'LEFT')
            ->join('permissionsgroup per', 'per.id = s.permissionsgroup_id', 'LEFT')
            ->where('s.id', $now_staff)
            ->field('s.id,s.name,s.name_en,s.card,s.private_tel,s.sex,s.birthday,s.is_marry,s.native_place,s.residence,edu.grade,s.school,s.major,s.account,s.password,s.emergency_name,s.emergency_phone,s.work_tel,dep.name as department_id,dp.position_name,per.description,s.salary_card,s.bank_card_account,s.money,s.contract_content,s.security_content,s.entry_time,s.private_wechat,s.work_wechat,s.private_qq,s.work_qq,s.family_situation')
            ->select();
        return $staff_list;
    }

    /*
     * 展示添加和修改的页面
     */
    public function show_add_edit($now_staff = "")
    {
        $department_list = db("Department")->select();
        $act = input("post.act");
        if ($act == "get_department_position") {
            $department_id = input("id");
            $department_position_list = db("departmentposition")->where("department_id", $department_id)->select();
            return $department_position_list;
        }
        $permission_list = db('permissionsgroup')->where('status', 1)->select();
        $education_list = db('education')->select();
        $departmentposition_list = db('departmentposition')->where('department_id', 3)->select();
        if (!empty($now_staff)) {
            $staff_list = db('staff')->where('id', $now_staff)->find();
            $departmentposition_list = db('departmentposition')->where('department_id', $staff_list['department_id'])->select();
            $this->assign([
                'is_add' => false
                , 'staff_list' => $staff_list
                , "department_list" => $department_list
                , 'permission_list' => $permission_list
                , 'education_list' => $education_list
                , 'departmentposition_list' => $departmentposition_list
            ]);
            return view("index/show_edit");
        } else {
            $this->assign([
                "department_list" => $department_list
                , 'permission_list' => $permission_list
                , 'education_list' => $education_list
                , 'departmentposition_list' => $departmentposition_list
                , 'is_add' => true
            ]);
            return view("index/show_add");
        }
    }

    /*
    * 检查是否重复
    */
    public function check_info()
    {
        $card = trim(input("now_card"));
        $res = db("staff")
            ->where('card', $card)
            ->value('name');
        return $res;
    }

    /*
     * 根据ID修改咨询者信息或添加咨询者信息
     */
    public function commit_add_edit()
    {
        $id = input("id");
        $name = trim(input("name"));
        $name_en = trim(input("name_en"));
        $card = trim(input("card"));
        $private_tel = trim(input("private_tel"));
        $sex = trim(input("sex"));
        $birthday = trim(input("birthday"));
        $is_marry = trim(input("is_marry"));
        $native_place = trim(input("native_place"));
        $residence = trim(input("residence"));
        $education_id = trim(input("education_id"));
        $school = trim(input("school"));
        $major = trim(input("major"));
        $emergency_name = trim(input("emergency_name"));
        $emergency_phone = trim(input("emergency_phone"));
        $account = trim(input("account"));
        $password = trim(input("password"));
        $work_tel = trim(input("work_tel"));
        $department_id = trim(input("department_id"));
        $department_position_id = trim(input("department_position_id"));
        $permissionsgroup_id = trim(input("permissionsgroup_id"));
        $salary_card = trim(input("salary_card"));
        $bank_card_account = trim(input("bank_card_account"));
        $money = trim(input("money"));
        $contract_content = trim(input("contract_content"));
        $security_content = trim(input("security_content"));
        $entry_time = trim(input("entry_time"));
        $private_wechat = trim(input("private_wechat"));
        $work_wechat = trim(input("work_wechat"));
        $work_wechat_password = trim(input("work_wechat_password"));
        $private_qq = trim(input("private_qq"));
        $work_qq = trim(input("work_qq"));
        $work_qq_password = trim(input("work_qq_password"));
        $family_situation = trim(input("family_situation"));
        $salary_card = trim(input("salary_card"));
        $salary_card = trim(input("salary_card"));
        $salary_card = trim(input("salary_card"));
        $head_img = trim(input("file_val"));
        $msg = "";
        if (empty($name) == 1) {
            $msg = '姓名不能为空';
        } else if (empty($card) == 1 || id_card($card) == 0) {
            $msg = '身份证号码格式错误';
        } else if (empty($birthday) == 1) {
            $msg = '请选择出生日期';
        } else if (empty($school) == 1) {
            $msg = '毕业学校不能为空';
        } else if (empty($account) == 1) {
            $msg = '员工账号不能为空';
        } else if (empty($password) == 1) {
            $msg = '员工密码不能为空';
        } else if (is_date($birthday) == 0 || Validate::is($birthday, 'date') == 0) {
            $msg = '出生日期格式错误';
        } else if (empty($private_qq) == 0 && is_num($private_qq) == 0) {
            $msg = '私人QQ号码格式错误';
        } else if (empty($work_qq) == 0 && is_num($work_qq) == 0) {
            $msg = '工作QQ号码格式错误';
        } else if (empty($salary_card) == 0 && is_num($salary_card) == 0) {
            $msg = '工资卡号格式错误';
        } else if (is_date($entry_time) == 0 || Validate::is($entry_time, 'date') == 0) {
            $msg = '入职时间格式错误';
        } else if (empty($private_tel) == 0 && is_phone($private_tel) == 0) {
            $msg = '私人电话号码格式错误';
        } else if (empty($emergency_phone) == 0 && is_phone($emergency_phone) == 0) {
            $msg = '紧急联系人号码格式错误';
        }
        if ($msg != "") {
            alert($msg, "error");
        }
        $info = [
            'name' => $name
            , "account" => $name
            , 'name_en' => $name_en
            , 'card' => $card
            , 'private_tel' => $private_tel
            , 'sex' => $sex
            , 'birthday' => $birthday
            , 'is_marry' => $is_marry
            , 'native_place' => $native_place
            , 'residence' => $residence
            , 'education_id' => $education_id
            , 'school' => $school
            , 'major' => $major
            , 'emergency_name' => $emergency_name
            , 'emergency_phone' => $emergency_phone
            , 'account' => $account
            , 'password' => $password
            , 'work_tel' => $work_tel
            , 'department_id' => $department_id
            , 'department_position_id' => $department_position_id
            , 'permissionsgroup_id' => $permissionsgroup_id
            , 'salary_card' => $salary_card
            , 'bank_card_account' => $bank_card_account
            , 'money' => $money
            , 'contract_content' => $contract_content
            , 'security_content' => $security_content
            , 'entry_time' => $entry_time
            , 'private_wechat' => $private_wechat
            , 'work_wechat' => $work_wechat
            , 'work_wechat_password' => $work_wechat_password
            , 'private_qq' => $private_qq
            , 'work_qq' => $work_qq
            , 'work_qq_password' => $work_qq_password
            , 'family_situation' => $family_situation
            , 'head_img' => $head_img
        ];

        if (empty($id)) {
            if (db('staff')->insert($info)) {
                alert("添加成功", "success", "show_list");
            } else {
                alert("添加失败", "error");
            }
        } else {
            if (db('staff')->where('id', $id)->update($info)) {
                alert("更新成功", "success", "/public_module/index/show_list");
            } else {
                alert("更新失败", "error");
            }

        }
    }

    /*
     * @param $now_staff     员工id
     */
    public function delete()
    {
        $id = input("id");
        $res = db("staff")->where('id', $id)
            ->update(['status' => "0"]);
        if ($res) {
            $status = 1;
            $msg = "删除成功";
        } else {
            $status = 0;
            $msg = "删除失败";
        }
        $return = [
            "status" => $status
            , "res" => $id
            , "msg" => $msg
        ];
        return $return;

    }

    /*
     * 展示恢复员工
     */
    public function show_restore()
    {
        $name = input("?name") ? input("name") : "";//搜索咨询者的名字
        $view = $this->view;
        $term['status'] = "0";
        $term['name'] = ['like', "%" . $name . "%"];
        $list = db("staff")->where($term)->order(['id' => "desc"])->paginate(6);
        $this->assign([
            'list' => $list
            , 'name' => $name
        ]);
        return view();
    }

    /*
	 * 根据ID恢复员工信息
	 * update()
	 */
    public function confirm_restore()
    {
        $id = input("id");
        $res = db("staff")->where('id', $id)
            ->update(['status' => "1"]);
        if ($res) {
            $status = 1;
            $msg = "恢复成功";
        } else {
            $status = 0;
            $msg = "恢复失败";
        }
        $return = [
            "status" => $status
            , "res" => $id
            , "msg" => $msg
        ];
        return $return;
    }
}
